package com.nuvsoft.android.nuvgamenet.example.buzzer.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.nuvsoft.android.nuvgamenet.GameLobby;
import com.nuvsoft.android.nuvgamenet.GameLobbyListAdapter;
import com.nuvsoft.android.nuvgamenet.NuvGameNetApplication;
import com.nuvsoft.android.nuvgamenet.R;
import com.nuvsoft.android.nuvgamenet.net.ConnectionChangeListener;

public class BuzzerLobbyList extends BuzzerActivity {
	//private static final String LOG_TAG = Buzzer.class.getSimpleName();
	private ListView lv;
	private ListAdapter lv_adapter;

	private ConnectionChangeListener cl;

	private NuvGameNetApplication na;

	private Handler messageHandler;

	public static final String lobbyListMessage = "lobby_list_changed";
	public static final String joinLobbyMessage = "start_lobby_activity"; 

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lobby_list);
		na = (NuvGameNetApplication) getApplication();
	}

	public void onStart() {
		super.onStart();

		lv = (ListView) findViewById(R.id.lv_open_lobbies);
		lv_adapter = new GameLobbyListAdapter(getApplicationContext(),
				R.layout.lobby_row, na.getConnectionManager().openLobbies);
		lv.setAdapter(lv_adapter);

		messageHandler = new Handler() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.Handler#handleMessage(android.os.Message)
			 */
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				if (msg.getData().containsKey(lobbyListMessage)) {
					if (lv_adapter != null)
						((ArrayAdapter<GameLobby>) lv_adapter)
								.notifyDataSetChanged();
				}
			}
		};

		cl = new ConnectionChangeListener() {
			@Override
			public void lobbyAdded() {
				Message m = new Message();
				m.getData().putBoolean(lobbyListMessage, true);
				messageHandler.sendMessage(m);
			}

			@Override
			public void lobbyRemoved() {
				Message m = new Message();
				m.getData().putBoolean(lobbyListMessage, true);
				messageHandler.sendMessage(m);
			}
			
			@Override
			public void joinLobby(){
				Intent myIntent = new Intent(BuzzerLobbyList.this, BuzzerLobby.class);
				//myIntent.putExtra("isHost", false);
				startActivity(myIntent);
				finish();
			}

			@Override
			public void playerAdded() {
				// do nothing
			}

			@Override
			public void playerRemoved() {
				// do nothing
			}

			@Override
			public void startGame() {
				// TODO Auto-generated method stud
			}

		};
		na.getConnectionManager().addConnectionListener(cl);
		na.getConnectionManager().startLobbyListener();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			na.getConnectionManager().removeConnectionListener(cl);
			na.getConnectionManager().stopLobbyListener();
			cl = null;
			finish();
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean enableDebugLog() {
		return true;
	}

	@Override
	public boolean enableVerboseLog() {
		return true;
	}

	@Override
	public String getLogTag() {
		return BuzzerLobbyList.class.getSimpleName();
	}
}
